<?php
class Jjjs
{
    
    static function get_order_obj($order_id)
    {
        $model = new Model();
        $order_obj = $model->table("order")->where("id=$order_id")->find();
        return $order_obj;
    }
    
    /**
     * 升级和保存每日新增业绩
     * @param unknown $user_id
     * @param unknown $order_id
     */
    static function sj_and_save_xzyj($user_id, $je, $from_info)
    {
        //1.先保存每日新增业绩
        //2.然后再升级
        self::save_xzyj($user_id, $je, $from_info);
        //self::sj($user_id);
    }
    
    /**
     * 升级
     */
    static function sj($user_id)
    {
        Common::auto_sj_user_to_hg($user_id);
    }
    
    /**
     * 保存每日新增业绩
     */
    static function save_xzyj($user_id,$je, $from_info)
    {        
        $xf_type = 1;   
        $status = 0;//表明为购物
        Common::save_every_day_xzyj($user_id, $je, $status, $from_info, $xf_type);
    }
    
    
    //购物后运行所有的奖项
    static function exec_all_jj($user_id, $je, $from_info)
    {
        //兑换时，赠送给正式会员的
        self::xfq_shopping_zs_point($user_id, $je, $from_info);
        //分享奖
        self::fxj($user_id, $je, $from_info);
        //管理奖
        self::glj($user_id, $je, $from_info);
    }
    
    /**
     * 得到1份分红奖的金额
     */
    static function get_one_fhj($now_date)
    {
        $every_day_fh_je = self::get_every_day_sf_zje($now_date);
        
        $all_dj_xfjf = self::get_all_dj_xfjf();
        if($all_dj_xfjf > 0 && $every_day_fh_je > 0)
        {
            return $every_day_fh_je / $all_dj_xfjf;
        }
        else
        {
            return 0;
        }
        
    }
    
    
    /**
     * 得到所有的冻结总积分
     */
    static function get_all_dj_xfjf()
    {
        $model = new Model();
        $res = $model->query("select sum(dj_xfjf) as total_je from zyk_customer");
        return $res[0]['total_je'];
    }
    
    /**
     * 判断用户是否有资格得到分红奖
     * 1.冻结
     * 2.以释放的 积分 》冻结积分
     * @param unknown $user_id
     */
    static function is_get_fhj($user_id)
    {
        $user_obj = Common::get_user_obj($user_id);
        if(intval($user_obj['nojj']) === 1)
        {
            return false;
        }
        
        if($user_obj['sf_xfjf'] >= $user_obj['dj_xfjf'])
        {
            return false;    
        }
        return true;
    }
    
    /**
     * 获得今天新增的消费业绩
     * @return number
     */
    static function get_day_add_xfyj()
    {
        $ndate = date('Y-m-d');
        $model = new Model();
        
        //当天的新增消费业绩
        $result1 = $model->query("select sum(xzyj) as total_je_1 from zyk_every_day_xzyj where date(create_time)='{$ndate}' and (xf_type=0 or xf_type=1)");
        $total_je1 = floatval( $result1[0]['total_je_1'] );
        return $total_je1;
    }
    
    static function get_final_je_1($now_date)
    {
        
        $model = new Model();
        
        //当天的新增消费业绩
        $result1 = $model->query("select sum(xzyj) as total_je_1 from zyk_every_day_xzyj where date(create_time)='{$now_date}' and (xf_type=0 or xf_type=1)");
        $total_je1 = floatval( $result1[0]['total_je_1'] );
        
        //每日新增消费额度的比例
        $bi_li_1 = Common::getConfigJjsetValue("jjset_fh_1")/100;
        
        $final_je_1 = $total_je1 * $bi_li_1;
        return $final_je_1;
    }
    
    
    static function get_final_je_2($now_date)
    {
        $model = new Model();
        //当天的反哺费
        $result2 = $model->query("select sum(xzyj) as total_je_2 from zyk_every_day_xzyj where date(create_time)='{$now_date}' and xf_type=2");
        $total_je2 = floatval( $result2[0]['total_je_2'] );
        
        //每日反哺费的比例
        $bi_li_2 = Common::getConfigJjsetValue("jjset_fbf");
        
        $final_je_2 = $total_je2 * $bi_li_2;
        return $final_je_2;
    }
    
    
    /**
     * 得到每天计算每份释放金额的总金额
     * 70%每天新增消费额度 + 30%的反哺费
     * 积分购买 + 消费券购买 + 反哺费
     */
    static function get_every_day_sf_zje($now_date)
    {
        //$now_date = date('Y-m-d');
        /*
        $is_sd_sf = intval( Common::getConfigJjsetValue("jjset_day_sf_zje_is_ok") );
        if($is_sd_sf === 1)
        {
            //从配置文件中找到手动释放金额
            $final_je_1 = Common::getConfigJjsetValue("jjset_day_sf_zje");
        }
        else 
        {
            */
            $final_je_1 = self::get_final_je_1($now_date);
        //}
        
        
        
        //$final_je_2 = self::get_final_je_2($now_date);
        $final_je_2 = 0;
        
        //每日需要分红的总金额
        $final_je = $final_je_1 + $final_je_2;
        
        
        
        return $final_je;
    }
    
    /**
     * 每天释放积分  
     * 静态奖金
     * 被冻结的用户的得不到奖金
     */
    static function every_day_auto_sf_point()
    {
        set_time_limit(0);
        $now_date = date('Y-m-d',time());
        $now_datetime = date('Y-m-d H:i:s',time());
        $now_h = date("H");
        $now_i = date("i");
        
        //每天23:40之后释放
        if($now_h >= 23 && $now_i>=40)
        {
            
            $model = new Model();
            $count = $model->table("sf_list")->where("date(create_time)='{$now_date}'")->count();
            
            //如果之前是放过，就不再重复释放
            if($count > 0)
            {
                die;
            }
            //$every_fh_je = self::get_one_fhj($now_date);
            $every_fh_je = floatval(Common::getConfigJjsetValue("jjset_sd_day_fhj_je"));
            
            if($every_fh_je > 0)
            {
                $list = $model->table("customer")->select();
                
                for($i=0,$c=count($list); $i<$c; $i++)
                {
                    $user_id = $list[$i]['user_id'];
                    
                    //判断该用户是否有资格获得分红奖
                    if( self::is_get_fhj($user_id) )
                    {
                        $dj_xfjf = $list[$i]['dj_xfjf'];
                        $fhj = $every_fh_je * $dj_xfjf;
                        
                        $model->query("start transaction");
                        //冻结积分不累减，只累加，
                        //可用积分 累加
                        //已释放积分 累加
                        $user_dj_xfjf = $list[$i]['dj_xfjf'];//冻结
                        $user_ok_xfjf = $list[$i]['ok_xfjf'];//可用
                        $user_sf_xfjf = $list[$i]['sf_xfjf'];//已释放
                        //如果已经释放的积分+分红奖 > 总冻结积分,那么分红奖 = 总冻结积分-已释放的积分
                        if($fhj + $user_sf_xfjf > $user_dj_xfjf)
                        {
                            $fhj = ($user_dj_xfjf-$user_sf_xfjf);
                        }
                        
                        $res1 = $model->table("customer")->data(['ok_xfjf'=>"`ok_xfjf`+$fhj",'sf_xfjf'=>"`sf_xfjf`+$fhj"])->where("user_id=$user_id")->update();
                        $data['user_id'] = $user_id;
                        $data['jjtime']  = $now_datetime;
                        $data['from']    = "每日释放分红奖";
                        $data['fhj']     = $fhj;
                        $res2 = $model->table("jjmx")->data( $data )->insert();
                        if($res1 && $res2)
                        {
                            $model->query("commit");
                        }
                        else
                        {
                            $model->query("rollback");
                        }
                    }
                    
                }
                
                $final_sf_je = self::get_every_day_sf_zje($now_date);
                if($final_sf_je > 0)
                {
                    //保存释放总记录
                    $model->table("sf_list")->data(['real_sf_je'=>$final_sf_je,'create_time'=>$now_date])->insert();
                }
                
                
            }
            
        }
        
    }
    
    
    /**
     * 管理奖 直接到帐到 账户余额
     * 购物时产生
     * 有极差
     * @param unknown $order_id
     */
    static function glj($user_id, $je, $from_info)
    {
        $user_obj = Common::get_user_obj($user_id);
        $tjr_obj = Common::get_user_obj($user_obj['tjr_id']);
        self::get_glj($tjr_obj['id'], $je, $from_info);
        
    }
    
    static function get_glj($user_id, $je, $from_info)
    {
        static $temp_bili = 0;
        $model = new Model();
        $user_obj = Common::get_user_obj($user_id);
        //会员没有被冻结  且 为合格会员 且 用户身份必须大于0，不能是普通会员
        //if(!empty($user_obj) && !Common::check_user_is_freeze($user_id) && intval($user_obj['is_hg'])===1 && $user_obj['u_lx'] > 0)
        if(!empty($user_obj) && intval($user_obj['is_hg'])===1 && $user_obj['u_lx'] > 0)
        {
            $u_lx = intval( $user_obj['u_lx'] );
            $bili = Common::getConfigJjsetValue("jjset_glj_".$u_lx)/100;
            if( !empty($bili) && $bili > $temp_bili )
            {
                $final_bili = $bili- $temp_bili;
                $temp_bili  = $bili;
                
                $jj = $je * $final_bili;
                if($jj > 0)
                {
                    $model->query("start transaction");
                    
                    $res1            = $model->table("customer")->data(['balance'=>"`balance`+$jj"])->where("user_id=$user_id")->update();
                    $data['user_id'] = $user_id;
                    $data['jjtime']  = date('Y-m-d H:i:s');
                    $data['glj']     = $jj;
                    $data['from']    = $from_info;
                    $res2            = $model->table("jjmx")->data( $data )->insert();
                    if($res1 && $res2)
                    {
                        $model->query("commit");
                    }
                    else
                    {
                        $model->query("rollback");
                    }
                }
            }   
        }
        
        $tjr_obj = Common::get_user_obj($user_obj['tjr_id']);
        
        if( !empty($tjr_obj) && $u_lx < 2)
        {
            self::get_glj($tjr_obj['id'], $je, $from_info);
        }
        
    }
    
    
    
    /**
     * 分享奖  到帐到 账户余额
     * 购物时产生,产生的奖金要消耗对应的推广额度
     * @param unknown $order_id
     */
    static function fxj($user_id, $je, $from_info)
    {
        //调用前先判断支付类型
        //必须是通过消费券购物时，才能产生分享奖
        //用户必须没有被冻结
        
        //会员没有被冻结且支付类型为消费券购物
        //if(!Common::check_user_is_freeze($user_id)       )//需要再加代码
        
        
            $model = new Model();
            $tjr_list = Common::get_dai_tjr_list($user_id, 2);
            
            for($i=0,$c=count($tjr_list); $i<$c; $i++)
            {
                $tjr_id = $tjr_list[$i]['id'];
                $t = $i+1;
                $bili = Common::getConfigJjsetValue("jjset_fxj_$t")/100;
                
                $fxj_je = $je * $bili;
                
                $model->query("start transaction");
                
                $data['user_id'] = $tjr_id;
                $data['jjtime']  = date('Y-m-d H:i:s');
                $data['fxj']     = $fxj_je;
                $data['from']    = $from_info;
                
                //计算得出剩余推广额度(总推广额度 - 已用的推广额度)
                //$extend_je = $tjr_list[$i]['extend_je']-$tjr_list[$i]['extend_used_je'];
                //已用的推广额度，和产生的奖金比较，灼烧制,取最小值到帐
                $extend_je = $tjr_list[$i]['extend_last_je'];
                
                //到帐金额和剩余推广额度进行比较
                if($fxj_je < $extend_je)
                {
                    $final_je = $fxj_je;
                }
                elseif($fxj_je > $extend_je)
                {
                    $final_je = $extend_je;
                }
                else 
                {
                    $final_je = $fxj_je;
                }
                //实际到帐的分享奖
                $data['real_fxj'] = $final_je;
                $res2 = $model->table("jjmx")->data( $data )->insert();
                
                //消耗对应的推广额度
                $res1 = $model->table("customer")->data(['balance'=>"`balance`+$final_je",'extend_last_je'=>"`extend_last_je`-$final_je",'extend_used_je'=>"`extend_used_je`+$final_je"])->where("user_id=$tjr_id")->update();
                
                if($res1 && $res2)
                {
                    $model->query("commit");
                }
                else
                {
                    $model->query("rollback");
                }
                
            }
        
        
    }
    
    
    
    
    /**
     * 必须是使用消费券购物
     * 使用消费券购物时，赠送各种积分
     */
    static function xfq_shopping_zs_point($user_id, $je, $from_info)
    {
          
            //调用前先判断支付类型
            $model = new Model();
            
            $user_obj = Common::get_user_obj($user_id);
            //推广额度
            $zs_bili_1 = Common::getConfigJjsetValue("jjset_shopping_zs_1");
            //冻结积分
            $zs_bili_2 = Common::getConfigJjsetValue("jjset_shopping_zs_2");
            //云积分
            $zs_bili_3 = Common::getConfigJjsetValue("jjset_shopping_zs_3");
            
            //必须是合格会员，购物时才赠送指定积分
            /*
            if(intval($user_obj['is_hg']) === 1)
            {
            */    
            
                $model->query("start transaction");
                //赠送2倍的推广额度
                $zs_extend_je   = $je * $zs_bili_1;
                //赠送4倍消费积分
                $zs_dj_xfjf     = $je * $zs_bili_2;
                //赠送0.5倍云积分
                $zs_cloud_point = $je * $zs_bili_3;
                
                $data=['extend_je'=>"`extend_je`+$zs_extend_je",'extend_last_je'=>"`extend_last_je`+$zs_extend_je",'dj_xfjf'=>"`dj_xfjf`+$zs_dj_xfjf",'cloud_point'=>"`cloud_point`+$zs_cloud_point"];
                
                $res1 = $model->table("customer")->data($data)->where("user_id=$user_id")->update();
               
                $demo['user_id']     = $user_id;
                $demo['jjtime']      = date('Y-m-d H:i:s');
                $demo['from']        = $from_info;
                $demo['extend_je']   = $zs_extend_je;
                $demo['dj_xfjf']     = $zs_dj_xfjf;
                $demo['cloud_point'] = $zs_cloud_point;
                
                $res2 = $model->table("jjmx")->data($demo)->insert();
                if($res1 && $res2)
                {
                    $model->query("commit");
                }
                else
                {
                    $model->query("rollback");
                }
                
            //}
       
        
    }
    
    
    
    /**
     * 注册赠送推广额度
     * 
     */
    static function reg_zs_extend_je($user_id)
    {
        $model = new Model();
        
        $model->query("start transaction");
        
        //从配置文件中获取注册赠送的推广额度
        $extend_je = floatval(Common::getConfigJjsetValue("jjset_reg_extend_je"));
        
        //注册成功后，总推广额度累加 ，剩余推广额度累加
        $res1 = $model->table("customer")->data(['extend_je'=>"`extend_je`+$extend_je",'extend_last_je'=>"`extend_last_je`+$extend_je"])->where("user_id=$user_id")->update();
        
        $data['user_id']   = $user_id;
        $data['from']      = '注册赠送';
        $data['extend_je'] = $extend_je;
        $data['jjtime']    = date("Y-m-d H:i:s");
        $res2 = $model->table("jjmx")->data( $data )->insert();
        
        if($res1 && $res2)
        {
            $model->query("commit");
            return true;
        }
        else
        {
            $model->query("rollback");
            return false;
        }
    }    
    
    
    
    
    
    
    /**
     * 每次购买东西后发货时，计算消费者需要返多少
     * @param unknown $order_amount
     * @param unknown $seller_id
     */
    /*
    static function js_seller_and_user_need_fan_je($order_id)
    {
        
        $model = new Model();
        $order_obj = self::get_order_obj($order_id);
        //消费者用户id
        $user_id      = $order_obj['user_id'];
        $seller_id    = $order_obj['seller_id'];
        //订单价格
        $order_amount = $order_obj['order_amount'];
        if($user_id > 0)
        {
            $user_fan_bei = Common::getConfigJjsetValue("jjset_fan_user_bei");
            $user_fan_je = $order_amount * $user_fan_bei;
            $mx1 = array();
            $mx1['user_id']     = $user_id;
            $mx1['je']          = $order_amount;
            $mx1['need_je']     = $user_fan_je;
            $mx1['order_id']    = $order_id;
            $mx1['create_time'] = date('Y-m-d H:i:s');
            $mx1['order_no']    = $order_obj['order_no'];
            $model->table("fan")->data($mx1)->insert();
        }
        if($seller_id > 0)
        {
            $seller_fan_bei = Common::getConfigJjsetValue("jjset_fan_seller_bei");
            $seller_fan_je = $order_amount * $seller_fan_bei;
            $mx2 = array();
            $mx2['user_id']     = $seller_id;
            $mx2['je']          = $order_amount;
            $mx2['need_je']     = $seller_fan_je;
            $mx2['order_id']    = $order_id;
            $mx2['create_time'] = date('Y-m-d H:i:s');
            $mx2['is_seller']   = 1;
            $mx2['order_no']    = $order_obj['order_no'];
            $model->table("fan")->data($mx2)->insert();
            
            //订单金额的85%秒到商家
            $miao_to_seller_bili = Common::getConfigJjsetValue("jjset_fan_seller")/100;
            $miao_to_seller_je   = $order_amount * $miao_to_seller_bili;
            
            $model->table("customer")->data(['balance'=>"`balance`+$miao_to_seller_je"])->where("user_id=$seller_id")->update();
            
            $pt_kou_je_1_bili     = Common::getConfigJjsetValue("jjset_kou_pt_1")/100;
            $pt_kou_je_1          = $pt_kou_je_1_bili * $order_amount;
            $pt_kou_je_2_bili     = Common::getConfigJjsetValue("jjset_kou_pt_2")/100;
            $pt_kou_je_2          = $pt_kou_je_2_bili * $order_amount;
            
            $mx = array();
            
            $mx['order_id']      = $order_id;
            $mx['order_amount']  = $order_amount;
            $mx['seller_id']     = $seller_id;
            $mx['fan_seller_je'] = $miao_to_seller_je;
            $mx['kou_je1']       = $pt_kou_je_1;
            $mx['kou_je2']       = $pt_kou_je_2;
            $mx['order_no']      = $order_obj['order_no'];//订单编号
            $mx['create_time']   = date('Y-m-d H:i:s');
            //保存扣除记录
            $model->table("pt_kou")->data($mx)->insert();
            
        }
        
        
    }
    */
    
    
    
    /**
     * 根据返现记录表方法每日返现，发放前进行判断
     * @param unknown $order_id
     */
    static function every_day_fan()
    {
        /*
        $model  = new Model();
        //$last_date = date("Y-m-d", strtotime("-1 day"));
        $n_date = date("Y-m-d");
        //查询创建时间小于当前时间(发货第二天发放)，发放时间为空或者不是今天的
        $list = $model->table("fan")->where("date(create_time)<'{$n_date}' and is_over=0 and already_je<need_je and (fan_time is NULL or date(fan_time)<'{$n_date}')")->select();
        
        if(!empty($list))
        {
            for($i=0,$c=count($list); $i<$c; $i++)
            {
                $id        = $list[$i]['id'];
                $user_id   = $list[$i]['user_id'];
                $is_seller = intval($list[$i]['is_seller']);
                $je        = $list[$i]['je'];
                $order_no  = $list[$i]['order_no'];
                //需要返还的金额
                $need_je    = $list[$i]['need_je'];
                
                //已经返还的金额
                $already_je = $list[$i]['already_je'];
                
                //如果是商家
                if( $is_seller === 1 )
                {
                    $seller_fan_bili_1 = Common::getConfigJjsetValue("jjset_xjq_2")/100;
                    $seller_fan_bili_2 = Common::getConfigJjsetValue("jjset_up_line_xjq_2")/100;
                    $seller_fan_bili_3 = Common::getConfigJjsetValue("jjset_down_line_xjq_2")/100;
                    
                    //现金券
                    $xjq           = $je * $seller_fan_bili_1;
                    //线上现金券
                    $up_line_xjq   = $je * $seller_fan_bili_2;
                    //线下现金券
                    $down_line_xjq = $je * $seller_fan_bili_3;
                    
                    $fan_je = $xjq + $up_line_xjq + $down_line_xjq;
                }
                elseif( $is_seller === 0 )//如果是消费者
                {
                    $user_fan_bili_1 = Common::getConfigJjsetValue("jjset_xjq_1")/100;
                    $user_fan_bili_2 = Common::getConfigJjsetValue("jjset_up_line_xjq_1")/100;
                    $user_fan_bili_3 = Common::getConfigJjsetValue("jjset_down_line_xjq_1")/100;
                    //现金券
                    $xjq           = $je * $user_fan_bili_1;
                    //线上现金券
                    $up_line_xjq   = $je * $user_fan_bili_2;
                    //线下现金券
                    $down_line_xjq = $je * $user_fan_bili_3;
                    
                    $fan_je = $xjq + $up_line_xjq + $down_line_xjq;
                }
                
                
                //返的金额+已经返的金额 必须小于 需要返的金额
                if($fan_je>0 &&  $fan_je + $already_je <= $need_je )
                {
                    //现金券到帐
                    $model->table("customer")->data(['balance'=>"`balance`+$xjq",'up_line_xjq'=>"`up_line_xjq`+$up_line_xjq",'down_line_xjq'=>"`down_line_xjq`+$down_line_xjq"])->where("user_id=$user_id")->update();
                    $model->table("fan")->data(['fan_time'=>date('Y-m-d H:i:s'),'already_je'=>"`already_je`+$fan_je"])->where("id=$id")->update();
                
                    $mx                  = array();
                    $mx['user_id']       = $user_id;
                    $mx['xjq']           = $xjq;
                    $mx['up_line_xjq']   = $up_line_xjq;
                    $mx['down_line_xjq'] = $down_line_xjq;
                    $mx['is_seller']     = $is_seller;
                    $mx['fan_time']      = date("Y-m-d H:i:s");
                    $mx['order_no']      = $order_no;//所属订单号
                    $model->table("fan_list")->data($mx)->insert();
                
                }
                else
                {
                    $model->table("fan")->data(['is_over'=>1])->where("id=$id")->update();
                }
                
            }    
        }
        */
    }
    
    
    
    
    
    
    
    
    
    
    /*
	//推荐奖和3代奖,$lx=1创客，2商家，3领购店，4代理
	public static function tjj_ckordp($user_id,$lx)
	{
	    
		$lyname=Common::getUid($user_id)['name'];
		$tjr_id=Common::getUid($user_id)['tjr_id'];
		$tjj_e=Common::getConfigJjsetValue("jjset_tjj".$lx)/100;//直推奖
		$ck_money=Common::getConfigJjsetValue("jjset_ck_chongzhi");//创客费用
		$dplx=Common::getDp($user_id)['dp_lx'];
		$dp_money=Zyk_tj::zyk_info('qylx',$dplx,'money');//商家费用
		$lgd_money=Common::getConfigJjsetValue("jjset_sqlgd_jmf");//领购店费用
		$dllx=Common::getAgent($user_id)['dl_lx'];
		$dl_money=Zyk_tj::zyk_info('agentlx',$dllx,'money');//代理费用

		$cx=Common::getConfigJjsetValue("jjset_cx")/100;//重消

		$model = new Model();
		switch ($lx) {
			case '1':
				$je=$ck_money;
				break;
			case '2':
				$je=$dp_money;
				break;
			case '3':
				$je=$lgd_money;
				break;
			case '4':
				$je=$dl_money;
				break;
		}
		$tjj=$je*$tjj_e;
		$ucx=$tjj*$cx;
		$hj=$tjj*(1-$cx);
		$uinfo=$model->table("user")->fields('id,nojj,u_lx')->where("id=".$tjr_id)->find();
		$nojj=$uinfo['nojj'];
		$ulx=$uinfo['u_lx'];//大于0为创客条件
		if ($tjr_id>0 && $ulx>0 && $nojj==0 && $hj>0) {
			$data = array('u_tjj'=>"`u_tjj`+$tjj",'u_cx'=>"`u_cx`+$ucx",'balance'=>"`balance`+$hj",'u_zsr'=>"`u_zsr`+$tjj");
			$model->table("customer")->data($data)->where("user_id=".$tjr_id)->update();
			$mx = array('user_id'=>$tjr_id,'from'=>$lyname,'tjj'=>$tjj,'cx'=>$ucx,'hj'=>$hj,'jjtime'=>date('Y-m-d H:i:s'));
			$model->table("jjmx")->data($mx)->insert();
		}
		
		if ($lx<3) {
			$tjj_ds=Common::getConfigJjsetValue("jjset_tjj_ds".$lx)/100;//3代提成
			$tjj=$je*$tjj_ds;
			$ucx=$tjj*$cx;
			$hj=$tjj*(1-$cx);
			for ($i=1,$tid=$tjr_id; $i <= 3; $tid=$ftjid,$i++) {
				if ($tid>0) {
					$tinfo=$model->table("user")->fields('id,tjr_id,nojj,u_lx')->where("id=".$tid)->find();
					$ftjid=$tinfo['tjr_id'];
					$nojj=$tinfo['nojj'];
					$ulx=$tinfo['u_lx'];//大于0为创客条件
					if ($ulx>0 && $nojj==0 && $hj>0) {
						$data = array('u_tjdsj'=>"`u_tjdsj`+$tjj",'u_cx'=>"`u_cx`+$ucx",'balance'=>"`balance`+$hj",'u_zsr'=>"`u_zsr`+$tjj");
						$model->table("customer")->data($data)->where("user_id=".$tid)->update();
						$mx = array('user_id'=>$tid,'from'=>$lyname." [$i代]",'tjdsj'=>$tjj,'cx'=>$ucx,'hj'=>$hj,'jjtime'=>date('Y-m-d H:i:s'));
						$model->table("jjmx")->data($mx)->insert();
					}
				}else break;
			}
		}
		
	}
	*/
	
	/**
	 * 客户购买商家东西，商家的推荐人上3代获得该营业额的提成（确认收货时调用/ucenter/order_sign）
	 */
    
    /*
	public static function tjj_buy($user_id,$je)
	{
		if($user_id != 0)
		{
			$tjid=Common::getUid($user_id)['tjr_id'];
			$model = new Model();
			$cx=Common::getConfigJjsetValue("jjset_cx")/100;
			for ($i=1,$tid=$tjid; $i <= 3; $tid=$ftjid,$i++) {
				if ($tid>0) {
					$tinfo=$model->table("user")->fields('id,tjr_id,nojj,u_lx')->where("id=".$tid)->find();
					$ftjid=$tinfo['tjr_id'];
					$nojj=$tinfo['nojj'];
					$ulx=$tinfo['u_lx'];//大于0为创客条件
					$tjj=(Common::getConfigJjsetValue("jjset_dsj".$i)/100)*$je;//商家销售产品上3代人提成
					$ucx=$tjj*$cx;
					$hj=$tjj*(1-$cx);
					if ($ulx>0 && $nojj==0 && $hj>0) {
						$data = array('u_xstc'=>"`u_xstc`+$tjj",'u_cx'=>"`u_cx`+$ucx",'balance'=>"`balance`+$hj",'u_zsr'=>"`u_zsr`+$tjj");
						$model->table("customer")->data($data)->where("user_id=".$tid)->update();
						$mx = array('user_id'=>$tid,'from'=>"商家销售 [$i代]",'xstc'=>$tjj,'cx'=>$ucx,'hj'=>$hj,'jjtime'=>date('Y-m-d H:i:s'));
						$model->table("jjmx")->data($mx)->insert();
					}
				}else break;
			}
		}
		
	}
	*/
    
    
	/**
	 * 客户购物，客户的3代推荐人得到积分奖励(前提：身份必须是创客)(确认收货时调用/class/Pointlog.php)
	 * 并得消费金额40%作为学习币   60%作为信用币
	 */
    /*
	public static function point_buy($user_id,$point,$je)
	{
	    
		$model = new Model();
		$tjr_id=Common::getUid($user_id)['tjr_id'];
		$lyname=Common::getUid($user_id)['name'];
		$gwj=Common::getConfigJjsetValue("jjset_gwj")/100;
		$xx=Common::getConfigJjsetValue("jjset_gw_xxb")/100;//学习币
		$xy=Common::getConfigJjsetValue("jjset_gw_xyb")/100;//信用币
		$cx=Common::getConfigJjsetValue("jjset_cx")/100;
		$gwtc=$gwj*$point;
		$xxb=$je*$xx;
		$xyb=$je*$xy;

		$ucx=$gwtc*$cx;
		$hj=$gwtc*(1-$cx);

		$model->table("customer")->data(array('xxb'=>"`xxb`+$xxb",'xyb'=>"`xyb`+$xyb"))->where("user_id=".$user_id)->update();
		for ($i=1,$tid=$tjr_id; $i <= 3; $tid=$ftjid,$i++) {
			if ($tid>0) {
				$tinfo=$model->table("user")->fields('id,tjr_id,nojj,u_lx')->where("id=".$tid)->find();
				$ftjid=$tinfo['tjr_id'];
				$nojj=$tinfo['nojj'];
				$ulx=$tinfo['u_lx'];//大于0为创客条件

				if ($ulx>0 && $nojj==0 && $hj>0) {
					$data = array('u_gwtc'=>"`u_gwtc`+$gwtc",'u_cx'=>"`u_cx`+$ucx",'balance'=>"`balance`+$hj",'u_zsr'=>"`u_zsr`+$gwtc");
					$model->table("customer")->data($data)->where("user_id=".$tid)->update();
					$mx = array('user_id'=>$tid,'from'=>'购物提成 [$i代]','gwtc'=>$gwtc,'cx'=>$ucx,'hj'=>$hj,'jjtime'=>date('Y-m-d H:i:s'));
					$model->table("jjmx")->data($mx)->insert();
				}
			}else break;
		}
		
	}
	*/
	
}